package com.main.tripplanner.persistent;

import java.io.IOException;
import java.io.StringWriter;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


public class PersistentEntity {
	protected int id;
	protected static ObjectMapper mapper = new ObjectMapper();
	protected static PrettyPrinter prettyPrinter = new DefaultPrettyPrinter();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public static <T> T fromJson(String serialized, Class<T> serializedClass) 
			throws JsonParseException, JsonMappingException, IOException {
		return mapper.readValue(serialized, serializedClass);
	}
	
	public String toJsonString(boolean pretty) {
		StringWriter sw  = new StringWriter();
		String jsonString = null;
		try {
			if (pretty) {
				mapper.writeValue(sw, this);
				jsonString = sw.toString();
			} else {
				jsonString = mapper.writeValueAsString(this);
			}
		}
		catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonString;
	}
}
